package Servlet;

import Damain.emp;
import Service.EmpAddService;
import Service.EmpAddServiceImpl;
import org.apache.commons.beanutils.BeanUtils;
import sun.font.BidiUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;

@WebServlet(name = "EmpAddServlet", urlPatterns = "/empAddServlet")
public class EmpAddServlet extends HttpServlet {
    /**
     * 添加员工
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private EmpAddService empAddService = new EmpAddServiceImpl();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获取参数
        Map<String, String[]> parameterMap = request.getParameterMap();
        emp emp = new emp();
        try {
            //封装到util阿帕奇提供的工具类会将获取到的数据封装到实体类中 参数分别是类型和 参数集合
            BeanUtils.populate(emp, parameterMap);
            empAddService.seve(emp);
            //转发到查询的servlet中 如果直接转发到员工查询页面不会添加数据
            request.getRequestDispatcher("/empListServlet").forward(request, response);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
